<?php
/**
 * Created by zhangyh.
 * User: zhangyh
 * Date: 2018/11/6
 * Time: 11:44
 */

namespace App\Service\Activity;


use App\Model\Imgs;
use App\Model\Like;
use App\Model\Member;
use App\Model\Record;
use App\Service\BaseService;
use  Illuminate\Support\Facades\Redis;
use DB;

class RecordService extends BaseService
{


    /**
     * 获取排行
     * @author 张永辉 2018年11月6日
     */
    public function ranking()
    {

        $pageSize = 10;
        if (empty($this->page)) {
            $this->page = 1;
        }
        $offset      = ($this->page - 1) * $pageSize;
        $rankingData = Redis::ZREVRANGE($this->rankingKey(), $offset, $offset + $pageSize, 'WITHSCORES');
        $mids        = $recordIds = [];
        foreach ($rankingData as $key => $val) {
            $key                  = explode('_', $key);
            $temp                 = [
                'mid' => $key[0],
                'rid' => $key[1],
                'num' => $val,
            ];
            $mids[]               = $key[0];
            $recordIds[]          = $key[1];
            $this->data['rank'][] = $temp;
        }
        //用户信息
        $memberData = Member::select(['id', 'nickname', 'headimgurl', 'mobile'])->find($mids);
        if ($memberData) {
            $memberData = $memberData->toArray();
        }
        $memberData = $this->dealKey($memberData);
        $recodeData = $this->record($recordIds);
        foreach ($this->data['rank'] as &$val) {
            $val['memberData'] = $memberData[$val['mid']]??[];
            $val['recode']     = $recodeData[$val['rid']]??[];
        }
        $this->data['memberData'] = $this->getMyrecord();
        return $this->data;
    }

    /**
     * 获取我的排名
     * @author 张永辉 2018年11月6日
     */
    public function getMyrecord()
    {
        $where  = [
            'activity_id' => $this->activity_id,
            'member_id'   => $this->mid
        ];
        $res    = Record::where($where)->first();
        $myRank = 0;
        if ($res) {
            $myRank = Redis::ZRANK($this->rankingKey(), $this->mid . '_' . $res->id);
        }

        $memberData         = Member::select(['id', 'nickname', 'headimgurl'])->find($this->mid)->toArray();
        $memberData['rank'] = $myRank;
        return $memberData;
    }


    public function rankingKey()
    {
        return 'activity:ranking:' . $this->activity_id;
    }


    /**
     * 获取参与记录
     * @author 张永辉 2018年11月6日
     */
    public function record($ids)
    {
        $recordData = Record::whereIn('id', $ids)->select(['id', 'img_ids'])->get()->toArray();
        $imgIds     = [];
        foreach ($recordData as $val) {
            $val['img_ids']     = explode(',', $val['img_ids']);
            $imgIds[$val['id']] = $val['img_ids'][0];
        }
        if ($imgIds) {
            $data = Imgs::select(['id', 'cdn_path', 'img_size'])->find(array_values($imgIds))->toArray();
            $data = $this->dealKey($data);
            foreach ($imgIds as $key => $val) {
                $imgIds[$key] = $data[$val]??[];
            }
        }
        return $imgIds;
    }

    /**
     * 参与详情
     * @author 张永辉 2018年11月6日
     */
    public function recordInfo()
    {
        $record            = Record::find($this->id)->toArray();
        $record['img_ids'] = explode(',', $record['img_ids']);
        $imgData           = Imgs::select(['id', 'cdn_path', 'img_size'])->get()->toArray();
        $imgData           = $this->dealKey($imgData);
        foreach ($record['img_ids'] as &$value) {
            $value = $imgData[$value]??[];
        }
        return $record;
    }

    /**
     * 喜欢
     * @author 张永辉 2018年11月6日
     */
    public function recordLike()
    {
        if (empty($this->page)) {
            $this->page = 1;
        }
        if ($this->page == 1) {
            $offset   = 0;
            $pageSize = 50;
        } else {
            $offset   = 50 + ($this->page - 1) * 150;
            $pageSize = 150;
        }
        $data = Like::where('record_id', $this->id)->take($pageSize)->skip($offset)->get(['head_img_url'])->toArray();
        return array_column($data, 'head_img_url');
    }

    /**
     * 参与记录
     * @author 张永辉 2018年11月6日
     */
    public function attendLog()
    {
        $now              = date('Y-m-d H:i:s');
        $sql              = 'SELECT a.* FROM ds_record as r LEFT JOIN ds_activity as a ON r.activity_id=a.id WHERE a.start_at<"' . $now . '" AND a.end_at>"' . $now . '" AND r.member_id=' . $this->mid;
        $nowData          = DB::select($sql);
        $sql              = 'SELECT a.* FROM ds_record as r LEFT JOIN ds_activity as a ON r.activity_id=a.id WHERE a.end_at<"' . $now . '" AND r.member_id=' . $this->mid;
        $overData         = DB::select($sql);
        $data['nowData']  = $nowData;
        $data['overData'] = $overData;
        return $data;
    }

}